package com.lx.util.secure.algorithm.coder;

import com.lx.annotation.Note;
import com.lx.util.secure.Algorithm;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class Url implements Coder{

    private Url() {}

    private static final Url URL = new Url();

    public static Url getCoder() {
        return URL;
    }


    @Override
    public String encodeToString(String data) {
        return URLEncoder.encode(data, StandardCharsets.UTF_8).replace("+", "%20");
    }

    @Override
    public byte[] encode(byte[] bytes) {
        return encode(new String(bytes,StandardCharsets.UTF_8));
    }

    @Override
    public byte[] encode(String data) {
        return encodeToString(data).getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public String encodeToString(byte[] bytes) {
        return encodeToString(new String(bytes,StandardCharsets.UTF_8));
    }


    @Override
    public String decodeToString(String data) {
        return URLDecoder.decode(data, StandardCharsets.UTF_8);
    }


    @Override
    public byte[] decode(byte[] bytes) {
        return decode(new String(bytes,StandardCharsets.UTF_8));
    }

    @Override
    public byte[] decode(String data) {
        return decodeToString(data).getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public String decodeToString(byte[] bytes) {
        return decodeToString(new String(bytes,StandardCharsets.UTF_8));
    }

}
